/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package crawler;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

/**
 *
 * @author Putra
 */
public class GetHTML implements Runnable {
    private String baseUrl;
    private String htmlText;
    private String targetUrl;
    public GetHTML(String baseUrl, String targetUrl){
        htmlText = "";
        this.baseUrl = baseUrl;
        this.targetUrl = targetUrl;
    }
    
    private void proses(){
        
        try {
            //System.out.println(this.hashCode());
            URL pageUrl = new URL(targetUrl);
            URLConnection conn = pageUrl.openConnection();
            conn.setAllowUserInteraction(false);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(pageUrl.openStream()));
            StringBuilder sb = new StringBuilder();
            String inputLine;
            while ((inputLine = bufferedReader.readLine()) != null) {
                sb.append(inputLine);
            }
            bufferedReader.close();
            htmlText = sb.toString();
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }

    /**
     * @return the htmlText
     */
    public String getHtmlText() {
        return htmlText;
    }

    /**
     * @return the baseUrl
     */
    public String getBaseUrl() {
        return baseUrl;
    }

    @Override
    public void run() {
        proses();
    }
}


